package com.tree.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tree.backend.common.Result;
import com.tree.backend.entity.dto.CustomerDemandDTO;
import com.tree.backend.entity.vo.CustomerDemandVO;
import com.tree.backend.service.ICustomerDemandService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequiredArgsConstructor
@RequestMapping("/customerDemand")
public class CustomerDemandController {
    private final ICustomerDemandService customerDemandService;

    @GetMapping("/list")
    public Result<Page<CustomerDemandVO>> getList(CustomerDemandDTO customerDemandDTO) {
        return customerDemandService.getList(customerDemandDTO);
    }


    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.success(customerDemandService.removeById(id));
    }

    @PutMapping
    public Result<Boolean> update(@RequestBody CustomerDemandDTO customerDemandDTO) {
        return customerDemandService.updateCustomerDemand(customerDemandDTO);
    }

    @PostMapping
    public Result<Boolean> add(@RequestBody CustomerDemandDTO customerDemandDTO) {
        // 对于单个添加的请求，将其转换为批量添加的方式处理
        // 这样可以确保使用相同的随机填充逻辑
        List<CustomerDemandDTO> dtoList = new ArrayList<>();
        dtoList.add(customerDemandDTO);
        return customerDemandService.batchAddWithRandomData(dtoList);
    }

    @PostMapping("/batchAdd")
    public Result<Boolean> batchAdd(@RequestBody List<CustomerDemandDTO> customerDemandDTOList) {
        return customerDemandService.batchAddWithRandomData(customerDemandDTOList);
    }
}
